package com.zhao.dota.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhao.dota.model.UserGold;
import com.zhao.dota.vo.UserAchievementVo;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserGoldMapper extends BaseMapper<UserGold> {

    @Select("SELECT u.nick_name, u.id, ug.gold, ug.init_gold, ug.league_id, g.name as `group`, t.win, t.lose," +
            "       ug.captain_mode_times, ug.double_times " +
            "  FROM user_gold ug " +
            "  LEFT JOIN user u " +
            "    ON ug.user_id = u.id " +
            "  LEFT JOIN league_user_group lug " +
            "    ON u.id = lug.user_id AND lug.league_id = #{leagueId}" +
            "  LEFT JOIN `group` g " +
            "    ON lug.group_id = g.id " +
            "  LEFT JOIN (SELECT lmu.user_id, sum( lmu.team = lm.winner ) win, sum( lmu.team <> lm.winner ) lose  " +
            "               FROM league_match_user lmu " +
            "               LEFT JOIN league_match lm ON lmu.league_match_id = lm.id" +
            "              WHERE lm.league_id = #{leagueId} " +
            "              GROUP BY lmu.user_id) t " +
            "    ON ug.user_id = t.user_id" +
            " WHERE ug.league_id = #{leagueId} " +
            " ORDER BY g.sort is null, g.sort asc ")
    List<UserAchievementVo> userGold(Integer leagueId);

}
